package com.example.kotlinExample.config

import com.example.kotlinExample.interceptor.UserInterceptor
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.http.converter.*
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.http.converter.xml.SourceHttpMessageConverter
import org.springframework.util.ResourceUtils
import org.springframework.web.client.RestTemplate
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import java.nio.charset.Charset
import java.util.*
import kotlin.collections.ArrayList


/**
 *
 *
 * @author liliang
 */
@Configuration
class WebConfig: WebMvcConfigurer {


    @Bean
    fun restTemplate(): RestTemplate {
        val requestFactory = SimpleClientHttpRequestFactory()
        requestFactory.setConnectTimeout(6000) // 设置建立连接超时时间  毫秒
        requestFactory.setReadTimeout(6000) // 设置读取数据超时时间  毫秒
        val restTemplate = RestTemplate(requestFactory)
//
//        // jackson配置
//        val objectMapper = ObjectMapper()
//        // 配置如果被序列化的对象没有可访问的属性  不报错，返回一个空的bean,即{}
//        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
//        // 创建json消息转换器
//        val jacksonConverter = MappingJackson2HttpMessageConverter(objectMapper)
//        val mediaTypes: MutableList<MediaType> = ArrayList(jacksonConverter.supportedMediaTypes)
//        // 为json消息转换器新增直接text_plain类型  避免遇到响应体是json,而响应头是text/plain类型无法找到适合的解析
//        mediaTypes.add(MediaType.ALL)
//        jacksonConverter.supportedMediaTypes = mediaTypes
//        val messageConverters: List<HttpMessageConverter<*>> = listOf(
//                ByteArrayHttpMessageConverter(),
//                StringHttpMessageConverter(Charset.forName("utf-8")),
//                ResourceHttpMessageConverter(),
//                SourceHttpMessageConverter(),
//                FormHttpMessageConverter(),
//                jacksonConverter
//        )
//        restTemplate.messageConverters = messageConverters
        return restTemplate
    }

    @Bean fun userInterceptor(): UserInterceptor {
        return UserInterceptor()
    }

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(userInterceptor())
                .addPathPatterns("/person/**")
                .excludePathPatterns("/static/")
    }

    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        // 配置模板资源路径
        registry.addResourceHandler("/templates/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/")
        registry.addResourceHandler("/static/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/")
    }
}